//
//  String+Extension.swift
//  Example
//
//  Created by 洪欣 on 2023/5/2.
//

import Foundation
import KeychainAccess
public enum ZFIconFont :String {
    case ic_search = "\u{e600}"
    case ic_scan = "\u{e601}"
    case ic_location = "\u{e603}"
    case ic_earth = "\u{e60f}"
    case ic_kefu = "\u{e604}"
    case ic_layer = "\u{e607}"
    case ic_close = "\u{e60b}"
    case ic_navigation = "\u{e611}"
    case ic_share = "\u{e612}"
    case ic_animation = "\u{e613}"
    case ic_street = "\u{e614}"
    case ic_vip = "\u{e618}"
    case ic_arrow_right = "\u{e61a}"
//    case ic_collection = "\u{e600}"
//    case ic_navigation = "\u{e601}"
//    case ic_3d = "\u{e602}"
//    case ic_2d = "\u{e603}"
//    case ic_sign = "\u{e604}"
//    case ic_layer = "\u{e606}"
//    case ic_street = "\u{e608}"
//    case ic_elevation = "\u{e609}"
//    case ic_camera = "\u{e605}"
//    case ic_air = "\u{e616}"
//    case ic_location_red = "\u{e61f}"
//    case ic_close = "\u{e617}"
//    case ic_wirte_location = "\u{e61b}"
//    case ic_wirte_add = "\u{e61c}"
//    case ic_wirte_vr = "\u{e61d}"
//    case ic_wirte_photo = "\u{e61e}"
//    case ic_operate_tool = "\u{e620}"
//    case ic_operate_service = "\u{e621}"
//    
//    case ic_draw_edit = "\u{e626}"
//    case ic_draw_muen = "\u{e628}"
//    case ic_draw_save = "\u{e638}"
//    case ic_draw_delete = "\u{e632}"
//    case ic_arrow_right = "\u{e624}"
//    case ic_draw_huitu = "\u{e63d}"
//    
//    case ic_draw_create = "\u{e633}"
//    case ic_draw_rollback = "\u{e634}"
//    case ic_draw_classes = "\u{e631}"
//    case ic_draw_border = "\u{e637}"
//    case ic_draw_color = "\u{e636}"
//    case ic_draw_back = "\u{e63a}"
//    case ic_draw_course = "\u{e63b}"
//    
//    case ic_draw_alert_picture = "\u{e629}"
//    case ic_draw_alert_gif = "\u{e62b}"
//    case ic_draw_alert_text = "\u{e62d}"
//    case ic_draw_alert_location = "\u{e62a}"
//    case ic_draw_alert_line = "\u{e62c}"
//    case ic_draw_alert_error = "\u{e635}"
//    case ic_draw_color_pen = "\u{e639}"
//    case ic_home_class_room = "\u{e63e}"
    
    

    
}

extension String{
    
    static func icon(icon:ZFIconFont) -> String{
        return icon.rawValue;
    }
}

extension String {
    
    subscript(_ indexs: ClosedRange<Int>) -> String {
            let beginIndex = index(startIndex, offsetBy: indexs.lowerBound)
            let endIndex = index(startIndex, offsetBy: indexs.upperBound)
            return String(self[beginIndex...endIndex])
        }
        
    subscript(_ indexs: Range<Int>) -> String {
        let beginIndex = index(startIndex, offsetBy: indexs.lowerBound)
        let endIndex = index(startIndex, offsetBy: indexs.upperBound)
        return String(self[beginIndex..<endIndex])
    }
    
    subscript(_ indexs: PartialRangeThrough<Int>) -> String {
        let endIndex = index(startIndex, offsetBy: indexs.upperBound)
        return String(self[startIndex...endIndex])
    }
    
    subscript(_ indexs: PartialRangeFrom<Int>) -> String {
        let beginIndex = index(startIndex, offsetBy: indexs.lowerBound)
        return String(self[beginIndex..<endIndex])
    }
    
    subscript(_ indexs: PartialRangeUpTo<Int>) -> String {
        let endIndex = index(startIndex, offsetBy: indexs.upperBound)
        return String(self[startIndex..<endIndex])
    }
    /// 判断是否是空符串（去除空格之后）
    public func isEmptyString() -> Bool {
        return self.trimmingAllWhiteSpaces().isEmpty
    }
    /// 去除字符串中的所有空格
    /// - Returns: 返回处理后的结果
    public func trimmingAllWhiteSpaces() -> String {
        let tempStr = self.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines)
        return tempStr.replacingOccurrences(of: " ", with: "")
    }
}


extension String {
    
    private static let KEYCHAIN_SERVICE:String = "com.linzaifei.map"
    private static let UUID_KEY = "com.linzaifei.map"
    
    static func UUID()->String{
        
        let keychain = Keychain(service: KEYCHAIN_SERVICE);
        var uuid:String = ""
        do {
            uuid = try keychain.get(UUID_KEY) ?? ""
        }catch let error {
            print(error)
        }
        if uuid.isEmpty {
            uuid = NSString.jk.stringWithUUID()!;
            do {
                try keychain.set(uuid, key: UUID_KEY)
            }
            catch let error {
                print(error)
                uuid = ""
            }
        }
    
        return uuid
    }
    
    /*
     例: sortValue(by:"1.2.3".versionCompare("4"))
     */
    func versionCompare(_ otherVersion: String) -> ComparisonResult {
        // https://sarunw.com/posts/how-to-compare-two-app-version-strings-in-swift/ --> 博客地址,需要翻墙
        let versionDelimiter = "."
        
        // <1> 我们按句点 ( .)拆分版本。
        var versionComponents = self.components(separatedBy: versionDelimiter) // 通过"."拆分出 版本数字的数组 ["1", "2", "3"]
        var otherVersionComponents = otherVersion.components(separatedBy: versionDelimiter) // 通过"."拆分出 版本数字的数组 ["4"]
        
        // <2> 然后，我们找到我们将零填充的数字差异。
        let zeroDiff = versionComponents.count - otherVersionComponents.count // 因为做比较必须格式相同, 位数少的用"0"补位. 此方法为了得到需要补几个"0"
        
        // <3> 如果没有区别，我们不需要做任何事情，使用 simple .compare。
        if zeroDiff == 0 {
            // Same format, compare normally
            return self.compare(otherVersion, options: .numeric)
        } else {
            //<4> 我们填充一个缺失零的数组。
            // 根据 zeroDiff的个数,添加有几个0的数组
            let zeros = Array(repeating: "0", count: abs(zeroDiff)) // ["0", "0"]
            if zeroDiff > 0 {
                // <5> 我们将零填充数组添加到具有更少周期和零的版本中
                otherVersionComponents.append(contentsOf: zeros) // 把剩余的不足位数用"0" 补齐 ["4", "0", "0"]
            } else {
                versionComponents.append(contentsOf: zeros)
            }
            
            /*
             //<6> 我们使用数组组件从组件构建我们的版本并比较它们。这次它将具有相同的周期和位数。
             // 比较类型为 .numeric
             
             详细解析:
             1. versionDelimiter = "."
                versionComponents.joined(separator: versionDelimiter) ==> "1.2.3"
                joined ==>  数组的用法, 通过 "." 把数组中元素拼接成 字符串
             2. string.compare(...., options: .numeric)
                compare(...) ==> 字符串比较, 比较的类型为数字 .numeric
             
             caseInsensitive 不区分大小写
             literal 文字
             backwards 向后
             anchored 锚定
             numeric 数字
             
             */
            return versionComponents.joined(separator: versionDelimiter)
                .compare(otherVersionComponents.joined(separator: versionDelimiter), options: .numeric)
        }
    }
    
    
}
